<?php
/**
 * Application model for Cake.
 *
 * This file is application-wide model file. You can put all
 * application-wide model-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.app
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * Application model for Cake.
 *
 * Add your application-wide methods in the class below, your models
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.app
 */

App::import('Lib', 'LazyModel');
class AppModel extends LazyModel {
	var $actsAs = array('Containable');

	var $cacheQueries=true;

	/**
	* A workaround for CakePHP lack of support for recursive
	*/
	function updateAll($fields, $conditions = true, $recursive = null) {
		if (!isset($recursive)) {
			$recursive = $this->recursive;
		}

		if ($recursive == -1) {
			$this->unbindModel(array(
				'belongsTo' => array_keys($this->belongsTo),
        		'hasOne' => array_keys($this->hasOne)
			), true);
		}

	    $result=parent::updateAll($fields, $conditions);
	    
	    if ($recursive == -1) {
	    	$this->bindModel(array(
				'belongsTo' => array_keys($this->belongsTo),
        		'hasOne' => array_keys($this->hasOne)
			), true);
	    }
	    return $result;
	}
	
	/**
	 * 
	 * Cache find queries
	 */ 
    function find($conditions = null, $fields = array(), $order = null, $recursive = null) {
    	if (!empty($fields['cacheKey'])) {
    		$cacheConfig = 'query';
			if (!empty($fields['cacheConfig'])){
      			$cacheConfig = $fields['cacheConfig'];
    		}
    		
    		$cacheName = $this->name . '-' . $fields['cacheKey'];
			
    		if (($data = Cache::read($cacheName, $cacheConfig)) === false) {
				$data = parent::find($conditions, $fields, $order, $recursive);
				Cache::write($cacheName, $data, $cacheConfig);
			}
    	}else{
    		$data = parent::find($conditions, $fields, $order, $recursive);
    	}
    	
    	return $data;
    }
	
	function prepareFields($data,$modelField){
		if(is_int($data)){
			$data=array($this->name=>array($this->primaryKey=>$data));
		}
		
		$fields=array();
		$contain=array();
		
		if(!isset($data[$this->name][$this->primaryKey])) $data[$this->name][$this->primaryKey] =$this->id;
		
		
		foreach ($modelField as $model=>$_fields){
			if(is_array($_fields)){
				foreach ($_fields as $field=>$val){
					if(is_int($field)){
						$field=$val;
						if(!isset($data[$model][$field])){
							$contain[$model]=$field;
						}
					} else {
						$contain[$model][$field]=$val;
					}
					
				}
				
			}else{
				$field=$_fields;
				if(!isset($data[$this->name][$field])) $fields[]=$field;
			}
		}
				
		
		$conditions=array($this->name.'.'.$this->primaryKey=>$data[$this->name][$this->primaryKey]);
		
		if(!empty($fields)|| !empty($contain)) $data=array_merge_recursive($data,$this->find('first',compact('fields','contain','conditions')));
		
		return $data;
	}
	
	function optimize(){
		if(!empty($this->useTable)) {
			$db =& ConnectionManager::getDataSource($this->useDbConfig);
			$db->query('OPTIMIZE TABLE ' . $db->fullTableName($this) . ';');
		}
	}
}
